package main

import (
	"net/http"
	"fmt"
	"ego/eredis"
	"strings"
	"github.com/garyburd/redigo/redis"
)

func main() {

	http.HandleFunc("/login",login)//设置访问路由
	err:=http.ListenAndServe(":8080",nil)//1、注册监听
	if err !=nil{
		fmt.Println(err)
	}else {
		fmt.Println("服务器已启动")
	}
}

func login(response http.ResponseWriter,request *http.Request)  {
	request.ParseForm()//设置解析参数

	form:=request.Form
	if len(form) < 2 {
		fmt.Fprint(response,"请输入登录帐号或密码")
		return
	}

	username:=form.Get("username")
	password:=form.Get("password")

	if queryUser(username,password) {
		fmt.Fprint(response,"登录成功")
	}else {
		fmt.Fprint(response,"帐号或密码错误")
		return
	}

}

//查询redis数据库
//username=13229516405
//password=123456
func queryUser(username string,password string) bool  {
	client:=eredis.Client
	un,ue:=redis.String(client.Do("get","username"))
	if ue !=nil {
		return false
	}
	pw,pe:=redis.String(client.Do("get","password"))
	if pe !=nil {
		return false
	}

	if strings.EqualFold(un,username) && strings.EqualFold(pw,password) {
		return true
	}

	return false
}
